package dth.bean;

import java.sql.*;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;

import dth.dao.DealerDAO;
import dth.dao.FaqDAO;
import dth.entity.Dealer;
import dth.entity.Faq;
import dth.utils.DthUtil;

@ManagedBean(name = "dealerBean")
public class DealerBean {
	private int id = 0;
	private String name;
	private String address;
	private java.util.List<Dealer> dealers = new ArrayList<Dealer>();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAdress() {
		return address;
	}

	public void setAdress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public java.util.List<Dealer> getDealers() {
		DealerDAO dao = new DealerDAO();
		System.out.println("-------user id:" + LoginBean.id);
		dealers = dao.getDealerByID(LoginBean.id);
		return dealers;
	}

	public void setDealers(java.util.List<Dealer> dealers) {
		this.dealers = dealers;
	}

	public java.util.List<Dealer> getListDealer() {
		DealerDAO dealerDAO = new DealerDAO();
		return dealerDAO.getListDealer();
	}

	public DealerBean() {
		System.out.println("---request url:" + DthUtil.getRequestURL());
		if (DthUtil.getRequestURL().contains("dealer.xhtml")) {
			System.out.println("--vao day role:" + LoginBean.roleCode);
			if (LoginBean.roleCode != 1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		// TODO Auto-generated constructor stub
		System.out.println("--dealer bean");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:" + objectAction);
		if (objectAction != null) {
			if (objectAction.equals("delete")) {
				Object objectId = DthUtil.getRequest()
						.getParameter("dealer_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id delete:" + id);
			} else if (objectAction.equals("update")) {
				Object objectId = DthUtil.getRequest()
						.getParameter("dealer_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id edit dealer:" + id);
				for (Dealer dealer : getListDealer()) {
					if (dealer.getDealerId() == id) {
						this.name = dealer.getDealerName();
						this.address = dealer.getDealerAddress();
					}
				}
				System.out.println("-----name:" + name);
			}
		}
	}

	public void insert() {
		System.out.println("---id=" + id);
		try {
			DealerDAO dealerDAO = new DealerDAO();
			Dealer dealer1 = new Dealer();
			dealer1.setDealerId(id);
			dealer1.setDealerName(name);
			dealer1.setAddress(address);

			java.util.Date date = new java.util.Date();// lay ngay hien tai
			dealer1.setDateInsert(new Date(date.getTime()));
			int count = 0;
			if (id == 0)
				count = dealerDAO.insert(dealer1);
			else
				count = dealerDAO.update(dealer1);
			if (count > 0)
				DthUtil.showMessage("Thông Báo", "Đã insert thành công");
			else
				DthUtil.showMessage("Thông Báo", "Đã insert thất bại");
		} catch (Exception ex) {
			DthUtil.showMessage("Thông Báo", "Đã insert thất bại");
		}
	}

	public void delete() {
		DealerDAO dealerDAO = new DealerDAO();
		int count = dealerDAO.delete(id);
		if (count > 0)
			DthUtil.showMessage("Thông Báo", "Đã xóa thành công");
		else
			DthUtil.showMessage("Thông Báo", "Đã xóa thất bại");
	}

}
